Title: Automatic EU VAT Validation in WooCommerce

Publish Date: Mon, 04 Dec 2023 08:00:00 +0000

Categories: Programming

Tags: WooCommerce, WordPress

Content:

Running an e-commerce store can be challenging, especially when dealing with international sales and tax regulations. One particular difficult and time-consuming task that merchants from the European Union have is to validate VAT numbers provided by customers.



To ensure compliance and smooth operations, WooCommerce store owners can implement an automatic EU VAT number validation using the VIES system to validate VAT on checkout. Let's delve into a straightforward approach to integrating this into your WooCommerce setup.







Table of Contents



Table of ContentsUnderstanding the Basics: What is VIES?How to Implement VAT Validation in WooCommerceWrapping It Up: VAT Validation Made Easy



Understanding the Basics: What is VIES?



VIES (VAT Information Exchange System) is a search engine (not a database) owned by the European Commission. The system allows you to confirm the validity of a VAT number issued by any EU member state.



This validation is important for B2B transactions when you sell goods across international borders within the EU. By verifying VAT numbers, businesses registered for VAT can apply the zero VAT rate for intra-community supplies.



The VAT number validation form from VIES.



How to Implement VAT Validation in WooCommerce





You can automate this process with a simple snippet that hooks into WooCommerce. Add the following PHP code to your child theme's functions.php file or use a custom plugin for inserting snippets:



<?php
/**
 * @snippet       VAT Automatic Validation on Checkout
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/12/04/streamline-your-store-with-automatic-woocommerce-eu-vat-validation/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_action( 'woocommerce_checkout_process', 'nm_automatic_validation_eu_vat' );
function nm_automatic_validation_eu_vat() {
   $vat = str_replace( array( ' ', '.', '-', ',', ',' ), '', wc_clean( $_POST['billing_vat'] ) );
   if ( ! empty( $vat ) ) {
       $response = wp_remote_get( "http://ec.europa.eu/taxation_customs/vies/vatResponse.html?ms=" . substr( $vat, 0, 2 ) . "&iso=" . substr( $vat, 2 ) );
       
       if ( is_wp_error( $response ) ) {
           wc_add_notice( 'There was a problem connecting to the VAT validation service. Please try again later.', 'error' );
       } else {
           // Retrieve the body and parse the JSON response
           $body   = wp_remote_retrieve_body( $response );
           $result = json_decode( $body, true );

           // Check if the VAT number is valid
           if ( $result['isValid'] !== true ) {
               wc_add_notice( 'Please enter a valid VAT number.', 'error' );
           }
       }
   }
}



This snippet accomplishes a few key tasks:




It removes spaces, dots, dashes, and commas from the VAT input. VAT registration numbers are often in various formats, and this can help.



It makes a call to the VIES service using the wp_remote_get() function.



It checks the response for the confirmation string indicating a valid VAT number.



If the service is down or the VAT identification number is invalid, it shows an error message that alerts the customer on the checkout page.




Remember to change the field ID on line 10 from billing_vat to the ID of your custom VAT number field. You can add such a field with Checkout Fields Editor or via custom code.



Wrapping It Up: VAT Validation Made Easy



With this simple snippet, you can add a layer of automation and compliance to your WooCommerce checkout process. It simplifies EU VAT number check, and keeps your sales records accurate for the tax authorities.



While the snippet provided is a good solution, EU VAT laws and the VIES system may update or change. It's important to stay informed about such changes and adjust your code accordingly to ensure ongoing compliance and functionality.



In conclusion, adding automatic VAT validation is a smart move for WooCommerce stores operating within the EU. It can save you time, ensure compliance, and provide a better checkout experience for your B2B customers.
